package chaos.infinity.mixin.NoFloatCanExtend;

import chaos.infinity.mixin.NoFloatCanExtend.ExtendState_Mixin;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.FallingBlockEntity;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Iterator;

@Mixin(AbstractBlock.class)
public abstract class NoFloatButExtend_Mixin {
//    public NoFloatButExtend_Mixin(AbstractBlock.Settings settings) {
//        super();
//    }

    @Unique
    private static boolean canFallThrough(BlockState state) {
        return state.isAir() || state.isIn(BlockTags.FIRE) || state.isLiquid() || state.isReplaceable();
    }

    @Inject(method = "scheduledTick", at = @At("HEAD"))
    protected void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random, CallbackInfo ci) {
        // 排除基岩
        if (state.isOf(Blocks.BEDROCK)) {
            return;
        }
        // 检查方块下方是否可穿透，以及方块是否在世界底部之上
        if (canFallThrough(world.getBlockState(pos.down())) && pos.getY() >= world.getBottomY()) {
            // 创建下落方块实体
            FallingBlockEntity fallingBlockEntity = FallingBlockEntity.spawnFromBlock(world, pos, state);
            // 移除当前方块
            world.removeBlock(pos, false);

            // 计算方块延伸，是否该下落
//            checkExtend(world, pos);
        }
    }

//    @Unique
//    private void checkExtend(BlockState state, ServerWorld world, BlockPos pos, Random random) {
//        int i = calculateDistance(world, pos);
//        BlockState blockState = (BlockState)((BlockState)state.with(DISTANCE, i)).with(BOTTOM, this.shouldBeBottom(world, pos, i));
//        if ((Integer)blockState.get(DISTANCE) == 7) {
//            if ((Integer)state.get(DISTANCE) == 7) {
//                FallingBlockEntity.spawnFromBlock(world, pos, blockState);
//            } else {
//                world.breakBlock(pos, true);
//            }
//        } else if (state != blockState) {
//            world.setBlockState(pos, blockState, 3);
//        }
//    }
//
//    @Unique
//    public int calculateDistance(BlockView world, BlockPos pos) {
//        BlockPos.Mutable mutable = pos.mutableCopy().move(Direction.DOWN);
//        BlockState blockState = world.getBlockState(mutable);
//        int i = 7;
//        if (blockState.isOf(world.getBlockState(pos).getBlock())) {
//            i = (Integer)blockState.get(DISTANCE);
//        } else if (blockState.isSideSolidFullSquare(world, mutable, Direction.UP)) {
//            return 0;
//        }
//
//        // 前后左右上五个方向
//        Iterator var5 = Direction.Type.HORIZONTAL.iterator();
//
//        while(var5.hasNext()) {
//            Direction direction = (Direction)var5.next();
//            BlockState blockState2 = world.getBlockState(mutable.set(pos, direction));
//            if (blockState2.isOf(world.getBlockState(pos).getBlock())) {
//                i = Math.min(i, (Integer)((ExtendState_Mixin)(Object)blockState2).getDistance() + 1);
//                if (i == 1) {
//                    break;
//                }
//            }
//        }
//
//        return i;
//    }
//
//    @Unique
//    private boolean isSupportBlock(BlockState state, World world, BlockPos pos) {
//        // 这里可以添加更多支撑方块的判断条件
//        return state.isOf(Blocks.SCAFFOLDING) || state.isSideSolidFullSquare(world, pos, Direction.UP);
//    }
}
